import showConfigData from '../flowable/showConfig';

// 默认props
export const PanelProps = {
	modeler: {
		type: Object,
		required: true
	},
	element: {
		type: Object,
		required: true
	},
	categorys: {
		type: Array,
		default: () => []
	}
};

export default function (props) {
	const formData = ref({}),
		elementType = computed(() => {
			const bizObj = props.element.businessObject;
			return bizObj.eventDefinitions ? bizObj.eventDefinitions[0].$type : bizObj.$type;
		}),
		showConfig = computed(() => showConfigData[elementType.value] || {});

	['id', 'name'].forEach((key) => {
		watch(
			() => formData.value[key],
			(val) => {
				updateProperties({ [key]: val });
			}
		);
	});

	watch(
		() => formData.value.documentation,
		(val) => {
			if (!val) {
				updateProperties({ documentation: [] });
				return;
			}
			const documentationElement = props.modeler.get('moddle').create('bpmn:Documentation', { text: val });
			updateProperties({ documentation: [documentationElement] });
		}
	);

	function updateProperties(properties) {
		const modeling = props.modeler.get('modeling');
		modeling.updateProperties(toRaw(props.element), properties);
	}

	const dialogExecutionListener = ref(false),
		executionListenerLength = ref(0);

	function computedExecutionListenerLength() {
		executionListenerLength.value =
			props.element.businessObject.extensionElements?.values?.filter((item) => item.$type === 'flowable:ExecutionListener').length ?? 0;
	}

	function finishExecutionListener() {
		dialogExecutionListener.value = false;
		computedExecutionListenerLength();
	}

	const dialogSignal = ref(false),
		signalLength = ref(0);
	function computedSignalLength() {
		signalLength.value = props.element.businessObject.extensionElements?.values?.filter((item) => item.$type === 'bpmn:Signal').length ?? 0;
	}
	function finishSignal() {
		computedSignalLength();
	}

	return {
		formData,
		PanelProps,
		elementType,
		showConfig,
		updateProperties,

		dialogExecutionListener,
		executionListenerLength,
		finishExecutionListener,
		computedExecutionListenerLength,

		dialogSignal,
		signalLength,
		computedSignalLength,
		finishSignal
	};
}
